package com.example.servlet;

import com.example.dao.ClassDAO;
import com.example.dao.ClassDAOImpl;
import com.example.dao.StudentDAO;
import com.example.dao.StudentDAOImpl;
import com.example.model.Classes;
import com.example.model.Student;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/students")
public class StudentServlet extends HttpServlet {
    private StudentDAO studentDAO;
    private ClassDAO classDAO;

    @Override
    public void init() {
        studentDAO = new StudentDAOImpl(); // 初始化DAO
        classDAO = new ClassDAOImpl(); // 初始化 ClassDAO
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");

        if (action == null) {
            action = "list"; // 默认操作是列出所有学生
        }

        switch (action) {
            case "new":
                showNewForm(request, response);
                break;
            case "insert":
                insertStudent(request, response);
                break;
            case "delete":
                deleteStudent(request, response);
                break;
            case "edit":
                showEditForm(request, response);
                break;
            case "update":
                updateStudent(request, response);
                break;
            case "search":
                searchStudents(request, response);
                break;
            case "checkStudentId":
                checkStudentId(request, response);
                break;
            default:
                listStudents(request, response);
                break;
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response); // 将POST请求转发给doGet处理
    }

    // 显示所有学生列表
    private void listStudents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Student> students = studentDAO.findAll();
        request.setAttribute("students", students);
        request.getRequestDispatcher("/views/listStudents.jsp").forward(request, response);
    }

    // 显示添加学生表单
    private void showNewForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 查询所有班级
        List<Classes> classList = classDAO.findAll();

        // 将班级列表设置为请求属性
        request.setAttribute("classList", classList);

        // 转发到添加学生页面
        request.getRequestDispatcher("/views/addStudent.jsp").forward(request, response);
    }

    // 显示编辑学生表单
    private void showEditForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String studentId = request.getParameter("id");
        String studentpwd = request.getParameter("pwd");
        Student student = studentDAO.findByStudentIdAndPassword(studentId, studentpwd); // 密码为空，仅用于查询
        // 查询班级列表
        List<Classes> classList = classDAO.findAll();

        // 设置请求属性
        request.setAttribute("student", student);
        request.setAttribute("classList", classList);
        request.getRequestDispatcher("/views/editStudent.jsp").forward(request, response);
    }

    // 添加学生
    private void insertStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String studentId = request.getParameter("studentId");
        String studentName = request.getParameter("studentName");
        String studentPwd = request.getParameter("studentPwd");
        String sex = request.getParameter("sex");
        int classId = Integer.parseInt(request.getParameter("classId"));

        Student newStudent = new Student();
        newStudent.setStudentId(studentId);
        newStudent.setStudentName(studentName);
        newStudent.setStudentPwd(studentPwd);
        newStudent.setSex(sex);
        newStudent.setClassId(classId);

        studentDAO.insert(newStudent);
        response.sendRedirect("students"); // 重定向到学生列表
    }

    // 更新学生信息
    private void updateStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String studentId = request.getParameter("studentId");
        String studentName = request.getParameter("studentName");
        String studentPwd = request.getParameter("studentPwd");
        String sex = request.getParameter("sex");
        int classId = Integer.parseInt(request.getParameter("classId"));

        Student student = new Student();
        student.setStudentId(studentId);
        student.setStudentName(studentName);
        student.setStudentPwd(studentPwd);
        student.setSex(sex);
        student.setClassId(classId);

        studentDAO.update(student);
        response.sendRedirect("students"); // 重定向到学生列表
    }

    // 删除学生
    private void deleteStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String studentId = request.getParameter("id");
        studentDAO.delete(studentId);
        response.sendRedirect("students"); // 重定向到学生列表
    }

    // 查询学生信息
    private void searchStudents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取查询条件
        String studentId = request.getParameter("studentId");
        String studentName = request.getParameter("studentName");
        String sex = request.getParameter("sex");

        // 调用 DAO 方法查询学生信息
        List<Student> students = studentDAO.searchStudents(studentId, studentName, sex);

        // 设置请求属性
        request.setAttribute("students", students);

        // 转发到学生列表页面
        request.getRequestDispatcher("/views/listStudents.jsp").forward(request, response);
    }

    // 检查学号是否重复
    private void checkStudentId(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String studentId = request.getParameter("studentId");

        // 调用 DAO 方法检查学号
        boolean exists = studentDAO.checkStudentIdExists(studentId);

        // 返回 JSON 响应
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        out.print("{\"exists\": " + exists + "}");
        out.flush();
    }
}